﻿# coding=utf-8
import os
import subprocess
from distutils import command
from datetime import date

# path = '/Users/dayang/PycharmProjects/Devops'
# info = subprocess.check_output("sh {0}".format('t.sh，shell=True, cwd=path，stdout=subprocess.PIPE')
from flask_wtf import FlaskForm
from wtforms import SelectField, StringField, RadioField, SubmitField, SelectMultipleField
from wtforms.validators import DataRequired

# serverpath = "/root"
# servercode = os.path.join(serverpath, 'codes', '*')
# print(servercode)
# status = "green"
# for x in [1,  2, 0, 8]:
#     try:
#         print("x:%s" % x)
#         print(2 / x)
#     except Exception as e:
#         status = "red"
#         a = (str(e))
#         print("error")
#     if status == "red":
#         continue
# print(status)
# print("xxx:%s%s" % (x, status))
# print(2 / 0)

# print("x:%s%s" % (x, status))
# if x == 5:
#     status = "red"
# if status == "red":
#     continue

# serverfile = '/root/1.zip'
# serverpath = '/var'
# cmd = 'unzip %s -d %s' % (serverfile, serverpath)
# print(cmd)
# info = os.system(cmd)
# print(info)
# status = "green"
# scname = 'test.sh'
# basepath = '/Users/dayang/'
# scpath = os.path.join(basepath, scname)
# print(scpath)
# try:  # shell=true 表示 可执行多条命令
#     subprocess.check_output("sh {0}".format(scpath), shell=True, cwd=basepath).decode('utf-8')
# except Exception as e:
#     print(e)
#     status = "red"
#     # print("error")
#     # print("e:%s" % e)
# print(status)
# taskinfo = 'xxx'
# serverpath = "/root/123/"+taskinfo  # 服务器目录
# p1 = serverpath + '/abc'
# print(serverpath)
# print(p1)
# a = 10
# for x in range(5):
#     try:
#         print("x:%s" % x)
#         y = int(x) - 2
#         print("y:%s" % y)
#         print(2 / y)
#         print("a:%s" % a)
#     except Exception as e:
#         a = 0
#         if a == 0:
#             print("code:%s" % a)
#             continue

# print(2/0)
# x = 0
# for a in range(0,3):
#

# status = 0
# for x in range(0, 5):
#     print("x:%s" % x)
#     print("status:%s" % status)
#     if x == 2:
#         status = "red"
#         print(status)
#     continue

# tag = 'gw-prod-master-20210106211209'
# tag = tag + '-1'
#
#
# tagnu = (tag[-1])
# tagnu = int(tagnu) + 1
# print(tagnu)
#
# tag1 = tag[:-1] + "2"
# print(tag1)
#
# a = 1
# a = 2
# print("a %s" %a)
# dlist = [1, 2, 3]
# for a in dlist:
#     print(a)
#     if a == 2:
#         continue


# import settings as s
# from flask import Flask, render_template, request, redirect, url_for, json, jsonify
#
# app = Flask(__name__)
#
# from utils import shell

# status = "1"
# print(status)
# a = input("input:")
# if a == '2':
#     status = "green"
#     print(status)
# else:
#     print(status)
# status = "0"
# try:
#     print("hello")
#     status = "1"
#     printx(status)
# except Exception as e:
#     print("error")
#     status = "2"
#     print(status)


# cmd = '/Users/dayang/PycharmProjects/Devops/test.sh'
#
# shellresult = shell('sh %s' % cmd)
# # ret.returncode
# print("shellresult:%s" % shellresult)
# if shellresult.returncode == 0:
#     print("输出结果信息:%s" % shellresult.stdout)  # 输出结果信息
#     print("执行成功")
# else:
#     print("执行失败")
#     print(shellresult.stderr)  # 输出错误信息

# cmd = 'ifconfig'
# info = os.system(cmd)
# print(info)
# c = s.date
# print(c)
# a = '/root/webapp/'
# b = a + 'install.sh'
# print(b)
# Path1 = '/home'
# Path2 = 'develop'
# Path3 = 'code'
#
# Path20 = os.path.join(Path1, Path2, Path3)
# print('Path20 = ', Path20)


from sshclient import SSHProxy


# if __name__ == '__main__':
#     with SSHProxy('192.168.1.151', 22, 'root', '123456') as xssh:
#         shell = 'after_download.sh'
#         path = '/root/update/2021011423/gw-prod-master-20210114232018/scripts/'
#         p = os.path.join(path, shell)
#         # info = xssh.command('sh %s' % p)
#         info = xssh.command('pwd1')
#         print(info)
#         if info['code'] == 0:
#             print("ok")
#             print(info['result'])
#         else:
#             print("fasil")
# print(info['result'])
# source = '/Users/dayang/PycharmProjects/Devops/codes/2021010317/gw/master.zip'
# print(type(source))
# desc = '/root/' + 'a.zip'
# print(type(desc))
# xssh.upload(source, desc)
# info = xssh.command('unzip /root/a.zip -d /root/test')
# print(info)
# def cmd(command):
#     subp = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8")
#     subp.wait(2)
#     if subp.poll() == 0:
#         print(subp.communicate()[1])
#     else:
#         print("失败")
#
#
# cmd('ifconfig')
# b = subprocess.call(['ls', '-alh'])
# print("code:%s" % b)
# try:
#    info = subprocess.check_output("sh %s" % 't.sh', shell=True, cwd=path)

#     stderr=subprocess.STDOUT)
# except Exception as e:
#     print("error")
# a = subprocess.Popen('sh /Users/dayang/t.sh', shell=True, cwd='/Users/dayang')
# status = subprocess.call('sh /Users/dayang/t.sh')
# print(status)
# if status == 0:
#     print(status)
# print("a:%s" % a)
# b = (1, 2, 3)
# for a in b:
#     if a == 1:
#         print("当前a1:%s" % a)
#         print("1")
#     if a == 2:
#         print("当前a2:%s" % a)
#         print(2)
#     if a == 3:
#         print("当前a3:%s" % a)
#         print("3")
#     if a == 4:
#         print("当前a4:%s" % a)
#         print("4")
#     else:
#         print("当前a:%s" % a)
#         print(a)

class addprojects(FlaskForm, list):
    project = StringField(label='项目名称',
                          validators=[DataRequired()],
                          render_kw={
                              'placeholder': 'type',
                              'class': 'form-control'
                          })
    Repo = StringField(label='Git地扯',
                       validators=[DataRequired()],
                       render_kw={
                           'placeholder': 'ipaddr',
                           'class': 'form-control'
                       })
    evn = StringField(label='项目环境',
                      validators=[DataRequired()],
                      render_kw={
                          'placeholder': 'value',
                          'class': 'form-control'
                      })
    path = StringField(label='项目路径',
                       validators=[DataRequired()],
                       render_kw={
                           'placeholder': 'desc',
                           'class': 'form-control'
                       })
    servers = SelectMultipleField(label='服务器:',
                                  validators=[DataRequired('请选择设备组')],
                                  render_kw={
                                      'class': 'form-control',
                                  },
                                  choices=[(1, 'server1'), (2, 'v'), (3, '爱情')],
                                  default=3,
                                  coerce=int,
                                  )
    checkzip = RadioField('certification', choices=['option1', 'option2'])
    submit = SubmitField('提交', render_kw={
        'class': 'btn btn-primary'
    })


# test = addprojects()
# print(test.servers.choices)
# print(test.servers.choices)
# ips = [(11, '1情感'), (222, '2星座'), (333, '3爱情')]
#
# test1 = addprojects()
# print(test1.servers.choices)
#
# print(test1.servers.type)
# a = [(1, 'value'), (2, 'value2')]
# for row in a:
#     # print(row)
#     print(row[0].value)

# class test1():
#     test2 = 'aaa'
#
#
# a = test1()
# a.test2 = 'ccc'
# print(a.test2)

# d = []
# for x in range(3):
#     c = {"server": str(x)}
#     d.append(c)
# print(d)
# for row in d:
#     print(row['server'])
# print(row['dic1'])
# d = [{'pro1': 'hostname1'}, {'pro1': 'hostname2'}]
# for a in d:
#     print(a)
#     print(a['pro1'])

# a = ([('project', '11'), ('Repo', '22'), ('evn', '33'), ('path', '44'), ('servers', 'host1'), ('servers', 'host2'),
#       ('servers', 'xxx')])
# server = a[5:7]
# for a in server:
#     print(a[1])

# x = {'project': ['a'], 'Repo': ['ab'], 'evn': ['c'], 'path': ['d'], 'servers': ['host3', 'host4']}
# cc = (x['servers'])
# for c1 in cc:
#     print(c1)

